<?php
class News extends Model{
	private $_NewsId;
	private $_Title;
	private $_Subtitle;
	private $_Link;
	private $_Text;
	private $_Active;
	private $_Created;
	private $_UserId;
	private $_IsNews;
	private $_Type = array ('Entrevista', 'Noticia');
	private $_Images = array();
	private $_Audio = array();
	private $_Video = array();
	private $_Tag = array();
	
	public function getType(){
		return $this->_Type[$this->getIsNews()];
	}

	function __construct($arrInitialValues = null){
		parent::__construct();

		if (isset($arrInitialValues['news_id']))
			$this->setNewsId($arrInitialValues['news_id']);
		else
			$this->setNewsId(0);

		if (isset($arrInitialValues['title']))
			$this->setTitle($arrInitialValues['title']);
		else
			$this->setTitle('');

		if (isset($arrInitialValues['subtitle']))
			$this->setSubtitle($arrInitialValues['subtitle']);
		else
			$this->setSubtitle('');

		if (isset($arrInitialValues['link']))
			$this->setLink($arrInitialValues['link']);
		else
			$this->setLink('');

		if (isset($arrInitialValues['text']))
			$this->setText($arrInitialValues['text']);
		else
			$this->setText('');

		if (isset($arrInitialValues['active']))
			$this->setActive($arrInitialValues['active']);
		else
			$this->setActive(0);
			
		if (isset($arrInitialValues['created']))
			$this->setCreated($arrInitialValues['created']);
		else
			$this->setCreated(date('d/m/Y'));
			
		if (isset($arrInitialValues['user_id']))
			$this->setUserId($arrInitialValues['user_id']);
		else
			$this->setUserId(0);

		if (isset($arrInitialValues['images']))
			$this->setImages($arrInitialValues['images']);
		else
			$this->setImages(array());

		if (isset($arrInitialValues['audio']))
			$this->setAudio($arrInitialValues['audio']);
		else
			$this->setAudio(array());

		if (isset($arrInitialValues['video']))
			$this->setVideo($arrInitialValues['video']);
		else
			$this->setVideo(array());

		if (isset($arrInitialValues['tag']))
			$this->setTag($arrInitialValues['tag']);
		else
			$this->setTag(array());
			
		if (isset($arrInitialValues['is_news']))
			$this->setIsNews($arrInitialValues['is_news']);
		else
			$this->setIsNews(0);	
	}

	/* GETTERS AND SETTERS */
	public function setIsNews($value){
		$this->_IsNews = $value;
	}
	
	public function getIsNews(){
		return $this->_IsNews;
	}
	
	public function setNewsId($value){
		$this->_NewsId = $value;
	}
	
	public function getNewsId(){
		return $this->_NewsId;
	}
	
	public function setUserId($value){
		$this->_UserId = $value;
	}
	
	public function getUserId(){
		return $this->_UserId;
	}
	
	public function setCreated($value){
		$this->_Created = $value;
	}
	
	public function getCreated(){
		return $this->_Created;
	}
	
	public function setTitle($value){
		$this->_Title = $value;
	}
	
	public function getTitle(){
		return $this->_Title;
	}
	
	public function setSubtitle($value){
		$this->_Subtitle = $value;
	}
	
	public function getSubtitle(){
		return $this->_Subtitle;
	}
	
	public function setLink($value){
		$this->_Link = $value;
	}
	
	public function getLink(){
		return $this->_Link;
	}
	
	public function setText($value){
		$this->_Text = $value;
	}
	
	public function getText(){
		return $this->_Text;
	}
	
	public function getActive(){
		return $this->_Active;
	}
	
	public function setActive($value){
		$this->_Active = $value;
	}
	
	public function setImages($value){
		$this->_Images = $value;
	}
	
	public function getImages(){
		return $this->_Images;
	}

	public function setAudio($value){
		$this->_Audio = $value;
	}
	
	public function getAudio(){
		return $this->_Audio;
	}
	
	public function setVideo($value){
		$this->_Video = $value;
	}
	
	public function getVideo(){
		return $this->_Video;
	}
	
	public function setTag($value){
		$this->_Tag = $value;
	}
	
	public function getTag(){
		return $this->_Tag;
	}
	
	public function isActive(){
		return $this->getActive();
	}

	public static function getAll(){
		parent::requireDB();

		$query = '
			SELECT * 
			FROM news
			ORDER BY created DESC';

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}

	public function getNews(){
		$query = '
			SELECT * 
			FROM news
			WHERE news_id = ' . $this->getNewsId();

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}

	public function update(){
		$query = '
			UPDATE news
			SET title = "' . addslashes($this->getTitle()) .'",
				subtitle = "' . addslashes($this->getSubtitle()) . '",
				link = "' . $this->getLink() . '",
				text = "' . addslashes($this->getText()) . '",
				created = "' . $this->getCreated() . '",
				is_news = ' . $this->getIsNews() . ',
				active = ' . $this->getActive() . '
			WHERE news_id = ' . $this->getNewsId();

		$dbHelper = new DBHelper($query);
		return $dbHelper->getNumRows();
	}
	
	public function getLastNewsId(){
		$news_id = $this->getNewsId();
		if (!empty($news_id))
			return $news_id;
			
		$id = $this->ObtainId();
		return $id[0]['news_id'];
	}
	
	private function _insertResource($table, $mapped_fields){
		$query = '
			INSERT INTO ' . $table;
			
		$columns = '('; $values = '(';
		foreach ($mapped_fields as $column => $value){
			$columns .= $column . ',';
			$values .= $value . ',';
		}

		$columns = substr($columns, 0, strlen($columns) -1) . ')';
		$values = substr($values, 0, strlen($values) -1) . ')';			
		
		$query .= $columns . ' VALUES ' . $values;
		$dbHelper = new DBHelper($query);
		return $dbHelper->getNumRows();
		
	}

	public function InsertVideo($news_id, $link, $text, $tag){
		return $this->_insertResource('video', array('news_id' => $news_id, 'link' => '"' . $link . '"', 'text' => '"' . $text . '"', 'tag' => '"' . $tag . '"'));
	}

	public function InsertAudio($news_id, $name_file, $text, $tag){
		return $this->_insertResource('audio', array('news_id' => $news_id, 'name_file' => '"' . $name_file . '"', 'text' => '"' . $text . '"', 'tag' => '"' . $tag . '"'));
	}
	
	public function InsertImage($news_id, $name_file, $text, $tag){
		return $this->_insertResource('images', array('news_id' => $news_id, 'name_file' => '"' . $name_file . '"', 'text' => '"' . $text . '"', 'tag' => '"' . $tag . '"'));
	}

	public function InsertTag($news_id, $tag){
		return $this->_insertResource('tags', array('news_id' => $news_id, 'tag' => '"' . $tag . '"'));
	}
	
	private function ObtainId(){
		$query = '
			SELECT MAX(news_id) AS news_id
			FROM news';

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
	
	public function create(){
		$query = '
			INSERT INTO news
			(title, subtitle, link, text, created, user_id, active, is_news)
			VALUES
			("' . addslashes($this->getTitle()) . '", "' . addslashes($this->getSubtitle()) . '",
			"' . $this->getLink() . '", "' . addslashes($this->getText()) . '",
			"' . $this->getCreated() . '", ' . $this->getUserId()->getUserId() . ', ' . $this->getActive() . ', ' . $this->getIsNews() . ')';

		$dbHelper = new DBHelper($query);
		return $dbHelper->getNumRows();
	}
	
	private function changeStatus(){
		$query='
			UPDATE news
			SET active = ' . $this->getActive() . '
			WHERE news_id = ' . $this->getNewsId();

		$dbHelper = new DBHelper($query);
		return ($dbHelper->getNumRows() > 0);
	}
	
	public function activate(){
		$this->setActive(1);
		return $this->changeStatus();
	}
	
	public function delete(){
		$this->setActive(0);
		return $this->changeStatus();
	}
	
	private function _obtainResource($table){
		$query = '
			SELECT *
			FROM ' . $table . '
			WHERE news_id = ' . $this->getNewsId();
		
		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
	
	public function obtainImages(){
		return $this->_obtainResource('images');
	}
	
	public function obtainAudio(){
		return $this->_obtainResource('audio');
	}
	
	public function obtainTag(){
		return $this->_obtainResource('tags');
	}
	
	public function obtainVideo(){
		return $this->_obtainResource('video');
	}

	private function deleteFile($path){
		@unlink($path);		
	}
	
	public function deleteImage($name_file){
		$this->deleteFile(MAIN_IMAGES_NEWS_DIRECTORY . $name_file);		
	}
	
	public function deleteAudio($name_file){
		$this->deleteFile(MAIN_FILES_DIRECTORY . $name_file);
	}
	
	public function getImageWithId($id){
		return $this->_getResourceWithId('images', 'image_id', $id);	
	}

	public function getAudioWithId($id){
		return $this->_getResourceWithId('audio', 'audio_id', $id);
	}
	
	public function updateResourceData($table, $id, $text, $tag){
		switch($table){
			case 'images':
				return $this->_updateResourceData($table, $id, 'image_id', $text, $tag);
			case 'audio':
				return $this->_updateResourceData($table, $id, 'audio_id', $text, $tag);
			case 'video':
				return $this->_updateResourceData($table, $id, 'video_id', $text, $tag);
		}
	}
	
	function _updateResourceData($table, $id, $field_id, $text, $tag){
		$query = '
			UPDATE ' . $table . '
			SET tag = "' . $tag . '", text = "' . $text . '"
			WHERE ' . $field_id . ' = ' . $id;
		
		$dbHelper = new DBHelper($query);
		return ($dbHelper->getNumRows() > 0);
	}
	
	private function _getResourceWithId($table, $field_id, $id){
		$query = '
			SELECT *
			FROM ' . $table . '
			WHERE ' . $field_id . ' = ' . $id;
		
		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();		
	}
	
	public function clearImage($id){
		return $this->_deleteResource('images', 'image_id', $id);
	}
	
	public function clearAudio($id){
		return $this->_deleteResource('audio', 'audio_id', $id);
	}
	
	public function deleteVideo($id){
		return $this->_deleteResource('video', 'video_id', $id);
	}
	
	public function deleteTag($id){
		return $this->_deleteResource('tags', 'tag_id', $id);
	}

	private function _deleteResource($table, $field_id, $id){
		$query = '
			DELETE
			FROM ' . $table . '
			WHERE ' . $field_id . ' = ' . $id;

		$dbHelper = new DBHelper($query);
		return ($dbHelper->getNumRows() > 0);
	}

	public function updateTextImage($arrText){
		$query = '
			UPDATE images
			SET text = "' . $arrText[1] . '",
				tag = "' . $arrText[2] . '"
			WHERE image_id = ' . $arrText[0];

		$dbHelper = new DBHelper($query);
		return ($dbHelper->getNumRows() > 0);
	}
}

?>